//
//  SafeConstructDeviceViewController.m
//  Estate
//
//  Created by wangdongdong on 2017/9/29.
//  Copyright © 2017年 ttsx. All rights reserved.
//

#import "SafeConstructDeviceModel.h"
#import "TextFieldCell.h"
#import "SwitchCell.h"

#import "SafeConstructDeviceViewController.h"

@interface SafeConstructDeviceViewController ()<UITableViewDelegate, UITableViewDataSource>

// 表格
@property (strong, nonatomic) TPKeyboardAvoidingTableView *tableView;

// 数据源
@property (strong, nonatomic) SafeConstructDeviceModel *model;

@end


@implementation SafeConstructDeviceViewController

// 数据源
- (SafeConstructDeviceModel *)model {
    if (!_model) {
        _model = [[SafeConstructDeviceModel alloc] init];
    }
    return _model;
}

- (TPKeyboardAvoidingTableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[TPKeyboardAvoidingTableView alloc]initWithFrame:CGRectMake(0, 10, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 188) style:UITableViewStylePlain];
        _tableView.backgroundColor = UIColorFromRGB(0xf2f2f2);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.bounces = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [_tableView registerNib:[UINib nibWithNibName:@"TextFieldCell" bundle:nil] forCellReuseIdentifier:@"fieldCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"SwitchCell" bundle:nil] forCellReuseIdentifier:@"switchCell"];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00];
    [self.view addSubview:self.tableView];
}

// 添加通知 (暂存/提交检查信息)
- (void)viewWillAppear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveSupervisionSafetyInspection:) name:@"saveSafeCheck" object:nil];
}

// 移除通知 (暂存/提交检查信息)
- (void)viewWillDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"saveSafeCheck" object:nil];
}

#pragma mark - 提交保存监理安全巡查
- (void)saveSupervisionSafetyInspection:(NSNotification *)notification {
    // 接收通知来的信息
    NSDictionary *parameDic = [notification object];
    FMDatabase *db = [FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    DBHelper *dbhelper = [DBHelper help];
    
    // 字段及对应值
    NSDictionary *pragameDic = [NSDictionary dictionary];
    pragameDic = @{
                   @"ID":DEFISNUL([NSUUID UUID].UUIDString),
                   @"PRO_ID":DEF_PERSISTENT_GET_OBJECT(@"proid"),
                   @"BASIC_RECORD_ID":DEFISNUL(parameDic[@"safeBaseID"]),
                   @"RECORD_USER":DEFISNUL(DEF_PERSISTENT_GET_OBJECT(@"userid")),
                   @"RECORD_TYPE":DEFISNUL(@"SAFE_RECORD_03"),
                   @"RECORD_TITLE":DEFISNUL(parameDic[@"title"]),
                   @"RECORD_NUMBER":DEFISNUL(parameDic[@"recordNumber"]),
                   @"DIVICE_NAME":DEFISNUL(self.model.diviceName),
                   @"WORK_TYPE":DEFISNUL(self.model.workType),
                   @"USER_ADDR":DEFISNUL(self.model.userAddr),
                   @"IS_MEET_QULIFICATION":DEFISNUL(self.model.isMeetQulification),
                   @"IS_MEET_STANDARD":DEFISNUL(self.model.isMeetStandard),
                   @"IN_REGISTER":DEFISNUL(self.model.inRegister),
                   @"IS_AGREE":DEFISNUL(self.model.isAgree),
                   @"EXTEND_INFO":@"",
                   @"CREATE_USER_NAME":DEF_PERSISTENT_GET_OBJECT(@"loginuser"),
                   @"CREATE_TIME":DEFISNUL([LSUtility nowTime]),
                   @"CREATE_IP":DEFISNUL([LSUtility getIPAddress]),
                   @"MODIFY_USER_NAME":@"",
                   @"MODIFY_TIME":@"",
                   @"MODIFY_IP":@"",
                   @"LOCAL_MODIFY_USER_NAME":@"",
                   @"LOCAL_MODIFY_TIME":@"",
                   @"LOCAL_MODIFY_STATE":@"ADD",
                   };
    
    BOOL isAdd = [dbhelper DataBase:db insertKeyValues:pragameDic intoTable:SAFE_CONSTRUCT_DEVICE];
    if (isAdd) {
        NSLog(@"添加施工设备进场成功");
        
    } else {
        NSLog(@"添加施工设备进场失败");
    }
    
    // 刷新安全列表页
    [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshSafeList" object:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma MARK - UITableViewDelegate, UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 7;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSInteger row = indexPath.row;
    if (row == 7) {
        return 130;
        
    } else {
        return 44;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    if (row == 0) {
        TextFieldCell *fieldCell = [tableView dequeueReusableCellWithIdentifier:@"fieldCell" forIndexPath:indexPath];
        fieldCell.selectionStyle = UITableViewCellSelectionStyleNone;
        fieldCell.nameLabel.text = @"设备名称";
        if (self.model.diviceName && ![self.model.diviceName isEqualToString:@""]) {
            fieldCell.inputField.text = self.model.diviceName;
        }
        [fieldCell setAllBlock:^(NSString *str) {
            self.model.diviceName = str;
        }];
        return fieldCell;
        
    } else if (row == 1) {
        TextFieldCell *fieldCell = [tableView dequeueReusableCellWithIdentifier:@"fieldCell" forIndexPath:indexPath];
        fieldCell.selectionStyle = UITableViewCellSelectionStyleNone;
        fieldCell.nameLabel.text = @"规格类型";
        if (self.model.workType && ![self.model.workType isEqualToString:@""]) {
            fieldCell.inputField.text = self.model.workType;
        }
        [fieldCell setAllBlock:^(NSString *str) {
            self.model.workType = str;
        }];
        return fieldCell;
        
    } else if (row == 2) {
        TextFieldCell *fieldCell = [tableView dequeueReusableCellWithIdentifier:@"fieldCell" forIndexPath:indexPath];
        fieldCell.selectionStyle = UITableViewCellSelectionStyleNone;
        fieldCell.nameLabel.text = @"使用地点";
        if (self.model.userAddr && ![self.model.userAddr isEqualToString:@""]) {
            fieldCell.inputField.text = self.model.userAddr;
        }
        [fieldCell setAllBlock:^(NSString *str) {
            self.model.userAddr = str;
        }];
        return fieldCell;
        
    } else if (row == 3) {
        SwitchCell *switchCell = [tableView dequeueReusableCellWithIdentifier:@"switchCell" forIndexPath:indexPath];
        switchCell.selectionStyle = UITableViewCellSelectionStyleNone;
        switchCell.nameLabel.text = @"检验检测单位资格(有/无)";

        if (self.model.isMeetQulification && ![self.model.isMeetQulification isEqualToString:@""]) {
            if ([self.model.isMeetQulification isEqualToString:@"1"]) {
                switchCell.rightSwitch.on = YES;
                
            } else {
                switchCell.rightSwitch.on = NO;
            }
        }
        switchCell.allBlock = ^(NSString *str) {
            self.model.isMeetQulification = str;
        };
        return switchCell;
        
    } else if (row == 4) {
        SwitchCell *switchCell = [tableView dequeueReusableCellWithIdentifier:@"switchCell" forIndexPath:indexPath];
        switchCell.selectionStyle = UITableViewCellSelectionStyleNone;
        switchCell.nameLabel.text = @"检验检测情况(是否合格)";
        if (self.model.isMeetStandard && ![self.model.isMeetStandard isEqualToString:@""]) {
            if ([self.model.isMeetStandard isEqualToString:@"1"]) {
                switchCell.rightSwitch.on = YES;
                
            } else {
                switchCell.rightSwitch.on = NO;
            }
        }
        switchCell.allBlock = ^(NSString *str) {
            self.model.isMeetStandard = str;
        };
        return switchCell;
        
    } else if (row == 5) {
        SwitchCell *switchCell = [tableView dequeueReusableCellWithIdentifier:@"switchCell" forIndexPath:indexPath];
        switchCell.selectionStyle = UITableViewCellSelectionStyleNone;
        switchCell.nameLabel.text = @"登记情况(是否登记)";
        
        if (self.model.inRegister && ![self.model.inRegister isEqualToString:@""]) {
            if ([self.model.inRegister isEqualToString:@"1"]) {
                switchCell.rightSwitch.on = YES;
                
            } else {
                switchCell.rightSwitch.on = NO;
            }
        }
        switchCell.allBlock = ^(NSString *str) {
            self.model.inRegister = str;
        };
        return switchCell;
        
    } else {
        SwitchCell *switchCell = [tableView dequeueReusableCellWithIdentifier:@"switchCell" forIndexPath:indexPath];
        switchCell.selectionStyle = UITableViewCellSelectionStyleNone;
        switchCell.nameLabel.text = @"处理意见(是否同意)";
        
        if (self.model.isAgree && ![self.model.isAgree isEqualToString:@""]) {
            if ([self.model.isAgree isEqualToString:@"1"]) {
                switchCell.rightSwitch.on = YES;
                
            } else {
                switchCell.rightSwitch.on = NO;
            }
        }
        switchCell.allBlock = ^(NSString *str) {
            self.model.isAgree = str;
        };
        return switchCell;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

